package com.turnengine.client.local.turn.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.turn.bean.ITurnSummary;
import com.turnengine.client.local.turn.bean.TurnSummarySerializer;
import java.io.IOException;

/**
 * The Get Turn Summary Data Serializer.
 */
public class GetTurnSummaryDataSerializer extends AbstractCommandDataSerializer<ITurnSummary> implements IGetTurnSummaryDataSerializer {

	/**
	 * Creates a new Get Turn Summary Data Serializer.
	 */
	public GetTurnSummaryDataSerializer() {
		super(GetTurnSummary.COMMAND_ID);
	}

	@Override
	public ITurnSummary readReturnValue(IDataReader reader) throws IOException {
		ITurnSummary value = new TurnSummarySerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, ITurnSummary value) throws IOException {
		new TurnSummarySerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<ITurnSummary> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();

		// Create object
		GetTurnSummary object = new GetTurnSummary();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<ITurnSummary> argument) throws IOException {
		GetTurnSummary object = (GetTurnSummary) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
	}

	@Override
	public IExecutableCommandResponse<ITurnSummary> newResponse() {
		return new GetTurnSummaryResponse();
	}
}
